
	var gRisk 			= new Object();
	gRisk['quit'] 		= 0;
	gRisk['countries'] 	= new Object();
	gRisk['chat'] 		= "";
	gRisk['log'] 		= "";
	gRisk['armies']		= "";
	
	
	function riskquit() {
		if(gRisk['quit'] != 1) {
			gRisk['quit']=1;
			xajax_quit();
		}
	}	
	
	function init_intervalfunctions() {
		intervalfunctions();
	}	
	
	function intervalfunctions(){
		var xmsg = getMsg();				
		if(gRisk['quit'] == 0) {
			if( (xmsg == "chat") || (xmsg == 'spectate') ) {
				if(xmsg == "spectate") { 
					var tbval = 3; 
					xajax_check4update(xmsg,tbval,gRisk['countries']);
				} else {
					if(document.forms['main'].currentgame.value != "Starting") {
						var tbval = document.forms['source'].t.value;	
						xajax_check4update(xmsg,tbval,gRisk['countries']);
					}
				}
			}
		}
	}	
	
	setInterval("init_intervalfunctions();",5000);
	
			
	function DisplayBottom(bottomsrc) {
		var b = bottomsrc.split("?");
		var bottomsrc = b[0];
		var myargs = new Array();
		if(b.length > 1) {
			b = b[1].split("&");
			for(i=0; i < b.length; i++) {
				var tmpvar = b[i].split("=");
				myargs[i] = tmpvar[1];
			}
		}
		if(bottomsrc == "deploy.php") {
			xajax_deployForm();
		} else if(bottomsrc == "attack.php") {
			xajax_attackForm();
		} else if(bottomsrc == "fortify.php") {
			xajax_fortifyForm();
		} else if(bottomsrc == "takeover.php") {
			xajax_takeoverForm(myargs[0],myargs[1],myargs[2]);
		} else if (bottomsrc == "newturn.php") {
			xajax_newTurn();
		}else if (bottomsrc == "notmyturn.php") {
			displayNotmyturn();
		}
	}		
	
	function displayNotmyturn() {
		var r = "not my turn";
		document.getElementById('bottom').innerHTML = r;
	}
	
	function DeployArmies()  {
		if (isNaN(document.deploy.numarmies.value) || document.deploy.numarmies.value < 1) {
			alert('This is not a valid amount of armies.');
			document.deploy.numarmies.focus();
		} else {
			xajax_deploy(document.deploy.country.value,document.deploy.numarmies.value);
			document.deploy.send.disabled = true;
		}
	}
	
	function Attack()  {
		xajax_attack(document.attack.from.value, document.attack.to.value, document.attack.numarmies.value);
		document.attack.send.disabled = true;
	}
	
	function AttackTillEnd()  {
		xajax_attackTillEnd(document.attack.from.value, document.attack.to.value, document.attack.numarmies.value);
		document.attack.send.disabled = true;
	}

	function AttackDone()  {
		document.attack.send.disabled = true;
		document.attack.done.disabled = true;
		DisplayBottom('fortify.php');
		set_turnstatus('Fortify');
	}
   
	function Takeover() {
		xajax_takeover(document.takeover.from.value, document.takeover.to.value, document.takeover.numarmies.value);
		document.takeover.send.disabled = true;
	}		
	
	function Fortify()  {
		if (isNaN(document.fortify.numarmies.value) || document.fortify.numarmies.value < 1)  {
			alert('This is not a valid amount of armies.');
			document.fortify.numarmies.focus();
		} else {
			xajax_fortify(document.fortify.numarmies.value, document.fortify.from.value, document.fortify.to.value);
			document.fortify.send.disabled = true;
			document.fortify.skip.disabled = true;
		}
	}
  
	function FortifySkip()  {
		xajax_fortify(0, document.fortify.from.value, document.fortify.to.value);
		document.fortify.send.disabled = true;
		document.fortify.skip.disabled = true;
	}
  
	function SelCountry(cNum)  {			  
		var country = cNum;
		var daaction = get_turnstatus();
		if ( daaction == "Deploying" ) {
		   with (document.forms['deploy'].country) {
			for (i=0; i < length; i++)
				if (options[i].value == country) value = country;
			}		   
		} 
		if ( daaction == "Attacking" ) {
		   with (document.forms['attack']) {
				for (i=0; i < from.length; i++)
					if (from.options[i].value == country) from.value = country;
			}
			with (document.forms['attack']) {
				for (i=0; i < to.length; i++)
					if (to.options[i].value == country) to.value = country;
			}
		}
		if( daaction == "Fortify" ) {
		   if (document.forms['fortify'].flag.value == 0) {
				document.forms['fortify'].from.value = country;
				document.forms['fortify'].flag.value = 1;
			} else {
				document.forms['fortify'].to.value   = country;
				document.forms['fortify'].flag.value = 0;
			}		   
		}  
	}			  
	
	// CHAT
	function emptyDialog() {
		gRisk['chat'] 	= "";
	}
	
	function insertDialog(stamp,mydialog) {
		//spielen('dialogsound');
		gRisk['chat'] 	= "<font color=\"green\">" + stamp + "</font><br>  " + mydialog + '<br> \n' + gRisk['chat'];
	}
	
	function setDialog() {
		var mydiv 	=  document.getElementById('tabsource');
		var r = ''
		+ "<div  id=\"dadialog\" style='width:100%; height:150px; overflow:auto;'>"	+ gRisk['chat'] + "</div>"
		+ "<table width='100%' cellpadding=0 cellspacing=0 border=1  style='display:inline;' > "
		+ "<tr><td nowrap>"
		+ "<form name='msg' onsubmit='return false'  style='display:inline;'>"
		+ "<input type='button' 	name ='sTo'  value  ='To Everyone' style='width:70px;'>"
		+ "<input type='text' 		name ='msg'  value  =''  style='width:100px;text-align:left'>"
		+ "<input type='hidden' 	name ='to'   value  ='1'>"
		+ "<input type='button' 	value='Send' onclick='SendMsg()'   style='width:40px;'>"
		+ "</form>	</td></tr></table>";
		mydiv.innerHTML = r;
	}
	
	function SendMsg() {
		rsmsg = document.msg.msg.value;
		rsto  = document.msg.to.value;
		xajax_addDialog(rsmsg + '|' + rsto);
		document.forms['msg']['msg'].value = '';
	}

	function SpeakTo(player) {
		player.blur();
		if (player.className.substr(0, 1) == 'p') {
			if (player.id == '') { return false; }
			if (document.msg.to.value != '1') {
				var old = document.getElementById(document.msg.to.value);
				old.className = old.className.substr(1);
			}
			document.msg.to.value 		= player.id;
			document.msg.sTo.value 		= 'To ' + player.value;
			document.msg.sTo.className 	= player.className;
			player.className 			= 'h' + player.className;
		} else {
			document.msg.to.value 		= '1';
			document.msg.sTo.value 		= 'To Everyone';
			document.msg.sTo.className 	= 'pButton';
			player.className 			= player.className.substr(1);
		}
	}


	function keyhandler(e) {
		if (document.layers) {
			Key = e.which;
		} else {
			Key = window.event.keyCode;
		}
		if (Key == 13) {  SendMsg();  }
		if (Key == 116){  Key = 0;    }
	}
	//document.onkeydown = keyhandler;
	
	
	// LOG
	function emptyLog() {	
			gRisk['log'] = "";
	}
	
	function insertLog(stamp,mydialog) {
		gRisk['log'] ="<font color=\"green\">" + stamp + "</font> <br> " + mydialog + '<br>'+ gRisk['log'] ;
	}	
	
	function setLog() {	
		var mydiv = document.getElementById('tabsource');
		var r =  "<div  id=\"gamelog\"  style=\"overflow:auto;width:100%;height:170px;\">" + gRisk['log'] + "</div>";
		mydiv.innerHTML = r;
	}
	
	
	// ARMIES
	function insertArmies(numarmies,color) {
		gRisk['armies'] = new Array(numarmies,color);
	}
	
	function setArmies() {
		var numarmies 	= gRisk['armies'][0];
		var color 		= gRisk['armies'][1];
		var r 			= "";
		r += "<div align=\"center\" ><div align=\"center\"  style=\"width:100%;height:170px;\" >";
		r += "<br>You have<br><form name=\"alform\">";
		r += "<input type=\"text\" name=\"armiesleft\" style=\"font-size:42; text-align:center; color:"+color+"\"";
		r += "size=\"1\" value=\""+numarmies+"\">";
		r += "<br><br>armies left. </form></div></div>";  
		document.getElementById('tabsource').innerHTML = r;
	}
	
	// CARDS
	function emptyCards() {
		if (document.forms['source'].t.value == 2) {	
			var myoption	= document.forms['cards'].inhand;	
			myoption.length = 0;
		}
	}
	
	function setCard(num,symbol,name) { 
		if (document.forms['source'].t.value == 2) {	
			var myoption	= document.forms['cards'].inhand;	
			if(num == '0'){ name = 'Sie haben keine Karten.'; } else { name = '{'+symbol+'} '+name;}
			NeuerEintrag = new Option(name, num , false, false);
			myoption.options[myoption.length]   = NeuerEintrag;
		}
	}
  
	function TradeCards() {
	 var sel = getSelectedCards(document.getElementById('myhand').options);
	 if (sel.length == 3) {
	  var strSel = '';
	  for (var item in sel) { strSel += sel[item].value + ','; }
	  var cardsvalue = strSel.substring(0, strSel.length-1);
	  document.playerlist.trade.disabled = true;
	  xajax_trade(cardsvalue);
	 } else {
	  alert('Please choose exactly 3 cards.');
	 }
	}
	
	function selectCard(i) {
		var myoption = document.getElementById('myhand').options[i];
		if(myoption.selected == true) { 
			myoption.selected = false;
			document.getElementById('card'+i).style.border = '1px solid black';
		} else { 
			myoption.selected = true;
			document.getElementById('card'+i).style.border = '1px solid red';
		}		
	}	
	
	function getSelectedCards(opt) {
	 var selected = new Array();
	 var index = 0;
	 for (var intLoop = 0; intLoop < opt.length; intLoop++) {
	  if (opt[intLoop].selected) {
	   index = selected.length;
	   selected[index] = new Object;
	   selected[index].value = opt[intLoop].value;
	   selected[index].index = intLoop;
	  }
	 }
	 return selected;
	}	
	
	// COUNTRIES
	function setCountry(country,armies,r,g,b) {
		gRisk['countries'][country] = armies+'|'+r+'|'+g+'|'+b;
		updateCountry(country,armies,r,g,b);
	}			
			
	// GAME VARS
	function setTurnvars(turnstatus,fgcolor,bgcolor,username) {
		document.main.turnstatus.value 		= turnstatus;
		document.main.cturn.style.color 		= fgcolor;
		document.main.cturn.style.background = bgcolor;
		document.main.cturn.value 			= username;
	}
	
	function get_turnstatus(){
		result = document.main.turnstatus.value;
		return result;
	}
	
	function set_turnstatus(value){
		document.main.turnstatus.value = value;
	}
	
	function myturn(){
		if (window.top.document.main.myturn.value == 0) {
			window.top.focus();
			DisplayBottom('newturn.php');
		}
	}		
	
	function setPlayer(colornum,username,num) {
		var myfield     = "p" + colornum;
		if((username != '') && (num != '')) {
			document.playerlist[myfield].value 	= username;
			document.playerlist[myfield].id 	= num;
		}
	}
	
	function setPlayerAndCards(colornum,username,num,mine,cardsarray) {
		var mydiv     = "p" + colornum + '_cards';
		var myfield     = "p" + colornum;
		if((username != '') && (num != '')) {
			document.playerlist[myfield].value 	= username;
			document.playerlist[myfield].id 	= num;
		}
		if(document.getElementById(mydiv))  {
			if(mine == 'true')  {
				var r =""; 
				r += "<form id=\"cards\" name=\"cards\" style=\"display:inline;\">";
				r += "<select id=\"myhand\" name=\"inhand\" multiple style=\"width:0px;height:0px;border:0px\">";
				var mycards = "";
				if(cardsarray.length) {
				for(var i=0; i<cardsarray.length; i++) {
					var a = cardsarray[i];
					var num 	= a[0]; 
					var name 	= a[1];
					var symbol	= a[2];			
					var cardimg = "";
					if(symbol == 'I') {
						 cardimg = "<img src=\"maps/karte_soldat.jpg\" width=\"20px\" id='card" + i + "' style=\"border:1px solid black;\" onclick=\"selectCard(" + i + ");\">";
					} else if(symbol == 'A') {
						 cardimg = "<img src=\"maps/karte_reiter.jpg\" width=\"20px\" id='card" + i + "' style=\"border:1px solid black;\" onclick=\"selectCard(" + i + ");\">";
					} else if(symbol == 'C') {
						cardimg = "<img src=\"maps/karte_kanone.jpg\"  width=\"20px\" id='card" + i + "' style=\"border:1px solid black;\" onclick=\"selectCard(" + i + ");\">";
					} else if(symbol == 'W') {
						cardimg = "<img src=\"maps/karte_joker.jpg\"   width=\"20px\" id='card" + i + "' style=\"border:1px solid black;\" onclick=\"selectCard(" + i + ");\">";
					}
					mycards += cardimg;				
					r += "<option value=\""+num+"\">{"+symbol+"} "+name+"</option>";
				}
				}
				r += "</select>";
				r += "<div style=\"overflow:auto;width:100%;height:80px;padding-top:2px\" nowrap>"+mycards+"</div>";
				r += "</form>";
			} else {
				var r = "";
				var mycards = "";
				for(var i=0; i<cardsarray.length; i++) {	 
					mycards += "<img src=\"maps/karte_back.jpg\" width=\"20px\">";				
				}
				r += "<div  style=\"overflow:auto;width:100%;height:80px;padding-top:2px\" nowrap>"+mycards+"</div>";
							
			}
			document.getElementById(mydiv).innerHTML = r;
		}
	}	
	
	function startGame(){
		if(document.main.currentgame.value == "Starting") {
			alert('RISK Game has been started...');
			document.location.replace('risk.php?nojoin=true');
		}
	}
	
	function endGame(username){
		alert("The game was ended by " + username +".");
		document.location.replace('index.php');
	}
	
	function completeGame(){
		document.location.href ='win.php';
	}
	
	function dead(){
		alert("You're dead.\n\nClick Ok to enter spectator mode.");
		document.location.replace('spectate.php');
	}
	
	function kick()  {
		var agree=confirm('Are you sure you want to kick this player? This can not be undone!');
		if (agree) {
			var w = prompt('Enter a reason for kicking this player.', '');
			if (w != null) {
			  xajax_kick(document.forms['playerform'].player.value,w);
			}
		}
	}	
	
	function kickedBy(username){
		alert("You've been kicked by " + username); 
		document.location.replace('index.php');
	}
	
	function poke() {
		xajax_poke(document.forms['playerform'].player.value);
	}
	
	function pokedBy(username){
		alert("You've been poked by " + username); 
	}	
	
	function encodeURIComponent(s) {
		var st = escape(s);
		st = st.replace(/%21/g,'!');
		st = st.replace(/%28/g,'(');
		st = st.replace(/%29/g,')');
		st = st.replace(/%27/g,"'");
		st = st.replace(/%21/g,'!');
		st = st.replace('@','%40');
		st = st.replace('/','%2F');
		st = st.replace('+','%2B');
		return(st);
	}
		   
	function decodeURIComponent(s) {
		var st=s;
		st = st.replace('!','%21');
		st = st.replace('(','%28');
		st = st.replace(')','%29');
		st = st.replace("'",'%27');
		st = st.replace('!','%21');
		st = st.replace('%40','@');
		st = st.replace('%2F','/');
		st = st.replace('%2B','+');
		return(unescape(st));
	}
	
	function getMsg() {
		if(document.forms['xajaxmsgform']) {
			var xmsg = document.forms['xajaxmsgform'].elements['xajaxmsg'].value;		
		} else { var xmsg = ""; }
		return xmsg;
	}			

	function stoppen() 	{
		document.getElementById("ton1").innerHTML="";
	}
	
	function spielen(soundname) {
		document.getElementById("ton1").innerHTML
		 ="<embed src='sounds/dialogsound.mp3' loop=false type=\"application/x-mplayer2\" autostart=true  hidden=true NAME=\"sound1\"></embed>";
	}
